/*
 * USBSerialModule.cpp
 *
 *  Created on: Feb 23, 2012
 *      Author: xum
 */

#include "USBSerialModule.h"

int serIn; // var that will hold the bytes-in read from the serialB
char serialbuffer[50];
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte

USBSerialModule::USBSerialModule(unsigned long _rate):SenderModule(),ReceiverModule() {
	rate=_rate;
	Serial.begin(rate);
}

void USBSerialModule::sendMessage(Message& message) {
	Serial.println(message.getContent());
}

Message USBSerialModule::receiveMessage() {
	int sb = 0;
	serInIndx = 0;
	while (sb != 13&&sb!=10) {
		while (Serial.available() == 0) {
			delay(10);
		}
		if (Serial.available()) {
			sb = Serial.read();
			serialbuffer[serInIndx] = sb;
			serInIndx++;
			//serialWrite(sb); //optional confirmation
		}
//		Serial.println(sb);
	}
	serialbuffer[serInIndx-1] = '\0';
	String result = String(serialbuffer);
	return Message(result);
}

